<?php

namespace Apps\AppApi\Method\Activity;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Models\ActivityComment;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Libs\PicUrl;


/**
 * 活动的评论列表
 */
class GetComments extends BaseMethod
{
    public function run()
    {
        $id = intval($this->getDataItem('id', 0));
        $page = intval($this->getDataItem('page', 1));
        $limit = $this->getConfig()->limit;

        if ($id == 0) {
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, 'URL参数不全');
        }

        $list = ActivityComment::query()->columns([
            'comment_id',
            'comment_content',
            'addtime',
            'comment_user_id user_id',
            'u.user_nickname',
            'u.user_gender',
            'u.user_cover'
        ])->leftJoin('\Apps\Common\Models\UserBase', 'u.user_id = \Apps\Common\Models\ActivityComment.comment_user_id', 'u')->where('activity_id = ' . $id)->orderBy('addtime DESC')->limit($limit, ($page - 1) * $limit)->execute()->toArray();

        foreach ($list as $k => $val) {
            if (!preg_match('/^http/', $val['user_cover'])) {
                $list[$k]['user_cover'] = PicUrl::ActivityCover($val['user_cover'], $this->getDi());
            }
        }

        $this->setResult($list);
    }

}